<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>functions.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_functions_functions.js.html">functions.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilities for creating functions. Loosely inspired by the
<a name="line17"></a> * java classes: http://go/functions.java and http://go/predicate.java.
<a name="line18"></a> *
<a name="line19"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>
<a name="line23"></a>goog.provide(&#39;goog.functions&#39;);
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Creates a function that always returns the same value.
<a name="line28"></a> * @param {T} retValue The value to return.
<a name="line29"></a> * @return {function():T} The new function.
<a name="line30"></a> * @template T
<a name="line31"></a> */
<a name="line32"></a>goog.functions.constant = function(retValue) {
<a name="line33"></a>  return function() {
<a name="line34"></a>    return retValue;
<a name="line35"></a>  };
<a name="line36"></a>};
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Always returns false.
<a name="line41"></a> * @type {function(...): boolean}
<a name="line42"></a> */
<a name="line43"></a>goog.functions.FALSE = goog.functions.constant(false);
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Always returns true.
<a name="line48"></a> * @type {function(...): boolean}
<a name="line49"></a> */
<a name="line50"></a>goog.functions.TRUE = goog.functions.constant(true);
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Always returns NULL.
<a name="line55"></a> * @type {function(...): null}
<a name="line56"></a> */
<a name="line57"></a>goog.functions.NULL = goog.functions.constant(null);
<a name="line58"></a>
<a name="line59"></a>
<a name="line60"></a>/**
<a name="line61"></a> * A simple function that returns the first argument of whatever is passed
<a name="line62"></a> * into it.
<a name="line63"></a> * @param {T=} opt_returnValue The single value that will be returned.
<a name="line64"></a> * @param {...*} var_args Optional trailing arguments. These are ignored.
<a name="line65"></a> * @return {T} The first argument passed in, or undefined if nothing was passed.
<a name="line66"></a> * @template T
<a name="line67"></a> */
<a name="line68"></a>goog.functions.identity = function(opt_returnValue, var_args) {
<a name="line69"></a>  return opt_returnValue;
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>/**
<a name="line74"></a> * Creates a function that always throws an error with the given message.
<a name="line75"></a> * @param {string} message The error message.
<a name="line76"></a> * @return {!Function} The error-throwing function.
<a name="line77"></a> */
<a name="line78"></a>goog.functions.error = function(message) {
<a name="line79"></a>  return function() {
<a name="line80"></a>    throw Error(message);
<a name="line81"></a>  };
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Given a function, create a function that keeps opt_numArgs arguments and
<a name="line87"></a> * silently discards all additional arguments.
<a name="line88"></a> * @param {Function} f The original function.
<a name="line89"></a> * @param {number=} opt_numArgs The number of arguments to keep. Defaults to 0.
<a name="line90"></a> * @return {!Function} A version of f that only keeps the first opt_numArgs
<a name="line91"></a> *     arguments.
<a name="line92"></a> */
<a name="line93"></a>goog.functions.lock = function(f, opt_numArgs) {
<a name="line94"></a>  opt_numArgs = opt_numArgs || 0;
<a name="line95"></a>  return function() {
<a name="line96"></a>    return f.apply(this, Array.prototype.slice.call(arguments, 0, opt_numArgs));
<a name="line97"></a>  };
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * Given a function, create a new function that swallows its return value
<a name="line103"></a> * and replaces it with a new one.
<a name="line104"></a> * @param {Function} f A function.
<a name="line105"></a> * @param {T} retValue A new return value.
<a name="line106"></a> * @return {function(...[?]):T} A new function.
<a name="line107"></a> * @template T
<a name="line108"></a> */
<a name="line109"></a>goog.functions.withReturnValue = function(f, retValue) {
<a name="line110"></a>  return goog.functions.sequence(f, goog.functions.constant(retValue));
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Creates the composition of the functions passed in.
<a name="line116"></a> * For example, (goog.functions.compose(f, g))(a) is equivalent to f(g(a)).
<a name="line117"></a> * @param {function(...[?]):T} fn The final function.
<a name="line118"></a> * @param {...Function} var_args A list of functions.
<a name="line119"></a> * @return {function(...[?]):T} The composition of all inputs.
<a name="line120"></a> * @template T
<a name="line121"></a> */
<a name="line122"></a>goog.functions.compose = function(fn, var_args) {
<a name="line123"></a>  var functions = arguments;
<a name="line124"></a>  var length = functions.length;
<a name="line125"></a>  return function() {
<a name="line126"></a>    var result;
<a name="line127"></a>    if (length) {
<a name="line128"></a>      result = functions[length - 1].apply(this, arguments);
<a name="line129"></a>    }
<a name="line130"></a>
<a name="line131"></a>    for (var i = length - 2; i &gt;= 0; i--) {
<a name="line132"></a>      result = functions[i].call(this, result);
<a name="line133"></a>    }
<a name="line134"></a>    return result;
<a name="line135"></a>  };
<a name="line136"></a>};
<a name="line137"></a>
<a name="line138"></a>
<a name="line139"></a>/**
<a name="line140"></a> * Creates a function that calls the functions passed in in sequence, and
<a name="line141"></a> * returns the value of the last function. For example,
<a name="line142"></a> * (goog.functions.sequence(f, g))(x) is equivalent to f(x),g(x).
<a name="line143"></a> * @param {...Function} var_args A list of functions.
<a name="line144"></a> * @return {!Function} A function that calls all inputs in sequence.
<a name="line145"></a> */
<a name="line146"></a>goog.functions.sequence = function(var_args) {
<a name="line147"></a>  var functions = arguments;
<a name="line148"></a>  var length = functions.length;
<a name="line149"></a>  return function() {
<a name="line150"></a>    var result;
<a name="line151"></a>    for (var i = 0; i &lt; length; i++) {
<a name="line152"></a>      result = functions[i].apply(this, arguments);
<a name="line153"></a>    }
<a name="line154"></a>    return result;
<a name="line155"></a>  };
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Creates a function that returns true if each of its components evaluates
<a name="line161"></a> * to true. The components are evaluated in order, and the evaluation will be
<a name="line162"></a> * short-circuited as soon as a function returns false.
<a name="line163"></a> * For example, (goog.functions.and(f, g))(x) is equivalent to f(x) &amp;&amp; g(x).
<a name="line164"></a> * @param {...Function} var_args A list of functions.
<a name="line165"></a> * @return {function(...[?]):boolean} A function that ANDs its component
<a name="line166"></a> *      functions.
<a name="line167"></a> */
<a name="line168"></a>goog.functions.and = function(var_args) {
<a name="line169"></a>  var functions = arguments;
<a name="line170"></a>  var length = functions.length;
<a name="line171"></a>  return function() {
<a name="line172"></a>    for (var i = 0; i &lt; length; i++) {
<a name="line173"></a>      if (!functions[i].apply(this, arguments)) {
<a name="line174"></a>        return false;
<a name="line175"></a>      }
<a name="line176"></a>    }
<a name="line177"></a>    return true;
<a name="line178"></a>  };
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Creates a function that returns true if any of its components evaluates
<a name="line184"></a> * to true. The components are evaluated in order, and the evaluation will be
<a name="line185"></a> * short-circuited as soon as a function returns true.
<a name="line186"></a> * For example, (goog.functions.or(f, g))(x) is equivalent to f(x) || g(x).
<a name="line187"></a> * @param {...Function} var_args A list of functions.
<a name="line188"></a> * @return {function(...[?]):boolean} A function that ORs its component
<a name="line189"></a> *    functions.
<a name="line190"></a> */
<a name="line191"></a>goog.functions.or = function(var_args) {
<a name="line192"></a>  var functions = arguments;
<a name="line193"></a>  var length = functions.length;
<a name="line194"></a>  return function() {
<a name="line195"></a>    for (var i = 0; i &lt; length; i++) {
<a name="line196"></a>      if (functions[i].apply(this, arguments)) {
<a name="line197"></a>        return true;
<a name="line198"></a>      }
<a name="line199"></a>    }
<a name="line200"></a>    return false;
<a name="line201"></a>  };
<a name="line202"></a>};
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Creates a function that returns the Boolean opposite of a provided function.
<a name="line207"></a> * For example, (goog.functions.not(f))(x) is equivalent to !f(x).
<a name="line208"></a> * @param {!Function} f The original function.
<a name="line209"></a> * @return {function(...[?]):boolean} A function that delegates to f and returns
<a name="line210"></a> * opposite.
<a name="line211"></a> */
<a name="line212"></a>goog.functions.not = function(f) {
<a name="line213"></a>  return function() {
<a name="line214"></a>    return !f.apply(this, arguments);
<a name="line215"></a>  };
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Generic factory function to construct an object given the constructor
<a name="line221"></a> * and the arguments. Intended to be bound to create object factories.
<a name="line222"></a> *
<a name="line223"></a> * Callers should cast the result to the appropriate type for proper type
<a name="line224"></a> * checking by the compiler.
<a name="line225"></a> * @param {!Function} constructor The constructor for the Object.
<a name="line226"></a> * @param {...*} var_args The arguments to be passed to the constructor.
<a name="line227"></a> * @return {!Object} A new instance of the class given in {@code constructor}.
<a name="line228"></a> */
<a name="line229"></a>goog.functions.create = function(constructor, var_args) {
<a name="line230"></a>  /** @constructor */
<a name="line231"></a>  var temp = function() {};
<a name="line232"></a>  temp.prototype = constructor.prototype;
<a name="line233"></a>
<a name="line234"></a>  // obj will have constructor&#39;s prototype in its chain and
<a name="line235"></a>  // &#39;obj instanceof constructor&#39; will be true.
<a name="line236"></a>  var obj = new temp();
<a name="line237"></a>
<a name="line238"></a>  // obj is initialized by constructor.
<a name="line239"></a>  // arguments is only array-like so lacks shift(), but can be used with
<a name="line240"></a>  // the Array prototype function.
<a name="line241"></a>  constructor.apply(obj, Array.prototype.slice.call(arguments, 1));
<a name="line242"></a>  return obj;
<a name="line243"></a>};
</pre>


</body>
</html>
